package service

import (
	"gin_gorm_oj/modles"
	"github.com/gin-gonic/gin"
	"strconv"
)

func FollowList(c *gin.Context) {
	userID, err := strconv.Atoi(c.Query("user_id"))
	if err != nil {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "Invalid user_id",
		})
		return
	}

	var followListIdentity []modles.Relation
	if err := modles.DB.Where("user_identity = ?", userID).Find(&followListIdentity).Error; err != nil {
		c.JSON(500, gin.H{
			"code": 500,
			"msg":  err,
		})
		return
	}
	var userIDs []int
	for _, relation := range followListIdentity {
		userIDs = append(userIDs, relation.FollowIdentity) // Assuming FollowingID is the field representing followed user
	}
	var userList []modles.User
	if err := modles.DB.Where("identity IN (?)", userIDs).Find(&userList).Error; err != nil {
		c.JSON(500, gin.H{
			"code": 500,
			"msg":  "Failed to fetch users",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  userList,
	})

}
